package com.yc.damai.web;

import com.yc.damai.entity.Orderitem;
import com.yc.damai.entity.Orders;
import com.yc.damai.entity.Product;
import com.yc.damai.vo.Result;
import com.yc.damai.web.feign.IOrderAction;
import com.yc.damai.web.feign.IUserAction;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
public class IndexAction {

    @Resource
    private IOrderAction orderAction;
    @Resource
    private IUserAction userAction;

    @RequestMapping("addOrder")
    public Result addOrder( Orders orders) {
        System.out.println("订单信息");
        System.out.println(orders);
        // 保存订单
        Result res = orderAction.createOrder(orders);
        if(res.getCode() == 0){
            return res;
        }
        // 清空购物车
        res = userAction.clearCart(orders);
        if(res.getCode() == 0){
            return res;
        }
        // 减库存
        res = orderAction.updateCount(orders);
        if(res.getCode() == 0){
            return res;
        }
        return new Result(1, "下单成功!", null);
    }

    @Resource
    StringRedisTemplate stringRedisTemplate;

    @RequestMapping("startDaChu")
    public Result startDaChu(){
//        将数据库所有商品存入Redis
        List<Product> list = orderAction.queryAllProduct();
        list.forEach(p->{
            stringRedisTemplate.opsForHash().put(
                    "dachu", p.getPid()+"", p.getStore()+"");
        });
        return new Result(1, "大促启动!", null);
    }

    @RequestMapping("miaoSha")
    public Result miaoSha(String uid, String pid){
//        final Object p = stringRedisTemplate.opsForHash().get("miaoSha", uid);
//        if(p!=null){
//            return new Result(0, "你已经秒过该商品", null);
//        }
//        // 记录秒杀行为
//        stringRedisTemplate.opsForHash().put("miaoSha", uid, pid);

        String key = uid + "-" + pid;
        if(stringRedisTemplate.opsForSet().isMember("miaoShaSet", key)){
            return new Result(0, "你已经秒过该商品", null);
        }
        stringRedisTemplate.opsForSet().add("miaoShaSet", key);


        // 获取数量
        Object store = stringRedisTemplate.opsForHash().get("dachu", pid);
        // 数量减一
        store = Integer.parseInt(store.toString()) - 1 + "";
        // 存回redis
        stringRedisTemplate.opsForHash().put("dachu", pid, store.toString());

        // 消息队列: 发送订单消息
        sendMiaoShaMsg(Integer.parseInt(uid), Integer.parseInt(pid));

        return new Result(1, "秒杀成功!订单处理中", null);

    }

    @Resource
    private RabbitTemplate rabbitTemplate;

    public void sendMiaoShaMsg(int uid, int pid){
        // 构建秒杀的订单
        Orders orders = new Orders();
        // 省略读取用户信息
        orders.setUid(uid);
        orders.setState(1);

        Orderitem item = new Orderitem();
        // 省略读取商品价格
        item.setPid(pid);
        item.setCount(1);

        orders.setOrderitems(new ArrayList<>());
        orders.getOrderitems().add(item);

        // 调用下订单接口 ==> 通过消息队列
        rabbitTemplate.convertAndSend("miaosha", orders);  // 异步方式
        // Result  res = addOrder( orders);                          // 同步方式
    }

    @RabbitListener(queuesToDeclare = @Queue("miaosha"))
    public void handleMiaoShaMsg(Orders orders){
        Result  res = addOrder( orders);
        if(res.getCode() == 1){
            System.out.println("通知订单处理成功!  回调方法通知用户");
        } else {
            System.out.println("通知订单处理失败!  回调方法通知用户");
        }
    }

}
